package com.bsoft.gol.hcs.common.dao;

import com.bsoft.gol.hcs.common.entity.MsgRecord;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.StatelessSession;
import org.hibernate.transform.Transformers;
import com.bsoft.gol.hcs.utils.CommonUtils;

import java.util.List;
import java.util.Map;

/**
 * @description:消息Dao层
 * @author: zhengcb10708
 * @create: 2021-02-07 11:29
 **/
public  abstract class MsgRecordDao extends HibernateSupportDelegateDAO<MsgRecord> {
    public MsgRecordDao() {
        this.setKeyField("messageRecordId");
    }

    public List<Map<String, Object>> updateReadFlagByBusinessType(final String userId,final String businessType,final String visitOrganization) {
        final HibernateStatelessResultAction<List<Map<String, Object>>> action = new AbstractHibernateStatelessResultAction<List<Map<String, Object>>>() {
            @Override
            @SuppressWarnings("unchecked")
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append(" update MsgRecord set status='1'   " );
                stringBuffer.append(" where  uid=:userId and businessType=:businessType and status='0'  ");
                if(CommonUtils.isNotEmpty(visitOrganization)){
                    stringBuffer.append(" and parameters like '%\"orgId\":\""+visitOrganization+"\"%' ");
                }
                Query query = ss.createQuery(stringBuffer.toString());
                query.setString("userId", userId);
                query.setString("businessType", businessType);
                query.executeUpdate();
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    public List<Map<String, Object>> getMsgCountByBusinessType(final String businessType, final String userId , final String visitOrganization) {
        final HibernateStatelessResultAction<List<Map<String, Object>>> action = new AbstractHibernateStatelessResultAction<List<Map<String, Object>>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append(" SELECT  t.business_type as businessType, count(*) as count from  msg_record t where  t.status='0' and t.event='1'  " );
                if(CommonUtils.isNotEmpty(businessType)){
                    stringBuffer.append(" and  t.business_type='"+businessType+"' ");
                }
                if(CommonUtils.isNotEmpty(userId)){
                    stringBuffer.append(" and t.uid='"+userId+"' ");
                }
                if(CommonUtils.isNotEmpty(visitOrganization)){
                    stringBuffer.append(" and t.parameters  like '%\"orgId\":\""+visitOrganization+"\"%' ");
                }
                SQLQuery sqlQuery = ss.createSQLQuery(stringBuffer.toString());
                sqlQuery = (SQLQuery) sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
                List<Map<String, Object>> ls = (List<Map<String, Object>>) sqlQuery.list();
                setResult(ls);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }
}
